package com.yunxi.service.dubboService.impl;

import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.github.pagehelper.PageInfo;
import com.yunxi.core.base.Constant;
import com.yunxi.core.web.ApiResult;
import com.yunxi.dealer.pojo.PartnerStorageVO;
import com.yunxi.dealer.service.PartnerStorageDubboService;
import com.yunxi.model.order.OrderVehicle;
import com.yunxi.order.pojo.*;
import com.yunxi.order.service.OrderVehicleDubboService;
import com.yunxi.service.localService.OrderCustomerService;
import com.yunxi.service.localService.OrderVehicleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * <p>
 * 车辆订单 服务实现类
 * </p>
 *
 * @author
 * @since 2019-06-18
 */
/*@com.alibaba.dubbo.config.annotation.Service(version = "${demo.service.version}",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"
)*/
public class OrderVehicleDubboServiceImpl implements OrderVehicleDubboService {

   @Autowired
   @Qualifier(value = "orderVehicleServiceImpl")
   OrderVehicleService orderVehicleServiceImpl;

   @Autowired
   @Qualifier(value = "orderCustomerServiceImpl")
   OrderCustomerService orderCustomerService;

   @Autowired
   PartnerStorageDubboService partnerStorageDubboServiceImpl;

   @Override
   @Transactional
   @LcnTransaction
   public OrderVehicleVO getById(String id) throws InvocationTargetException, IllegalAccessException {
      OrderVehicle orderVehicle = orderVehicleServiceImpl.getById(id);
      OrderVehicleVO orderVehicleVO = new OrderVehicleVO();
      BeanUtils.copyProperties(orderVehicle, orderVehicleVO);
      return orderVehicleVO;
   }

   @Override
   @Transactional
   @LcnTransaction
   public void saveOrUpdate(OrderVehicleVO orderVehicleVO) throws InvocationTargetException, IllegalAccessException {
      OrderVehicle orderVehicle = new OrderVehicle();
      BeanUtils.copyProperties(orderVehicleVO, orderVehicle);
      orderVehicleServiceImpl.saveOrUpdate(orderVehicle);
   }

   @Override
   @Transactional
   @LcnTransaction
   public void removeById(String id) {
      orderVehicleServiceImpl.removeById(id);
   }

   @Override
   @Transactional
   @LcnTransaction
   public PageInfo<OrderVehicleVO> findPageByVO(OrderVehicleVO orderVehicleVO, Integer pageNum, Integer pageSize) throws InvocationTargetException, IllegalAccessException {
      PageInfo<OrderVehicleVO> pageInfo = orderVehicleServiceImpl.pageByVo(orderVehicleVO, pageNum, pageSize);
      return pageInfo;
   }

   @Override
   @Transactional
   @LcnTransaction
   public PageInfo<OrderVehicleVO> findPageByVO2(OrderVehicleVOReq orderVehicleVOReq, Integer pageNum, Integer pageSize) throws InvocationTargetException, IllegalAccessException {
      PageInfo<OrderVehicleVO> pageInfo = orderVehicleServiceImpl.relatePageList(orderVehicleVOReq, pageNum, pageSize);
      return pageInfo;
   }

   @Override
   @Transactional
   @LcnTransaction
   public void removeByListVo(List<OrderVehicleVO> orderVehicleVOList) throws IllegalAccessException, InvocationTargetException, InstantiationException {
      orderVehicleServiceImpl.removeByListVo(orderVehicleVOList);
   }

   @Override
   @Transactional
   @LcnTransaction
   public List listByVo(OrderVehicleVO orderVehicleVO) {
      return orderVehicleServiceImpl.listByVo(orderVehicleVO);
   }


   @Override
   @Transactional
   @LcnTransaction
   public PageInfo listVehicleDeliveryInfo(OrderVehicleDeliveryReqVO reqVO, Integer pageNum, Integer pageSize) {

      return orderVehicleServiceImpl.listVehicleDeliveryInfo(reqVO,pageNum,pageSize);
   }

   /**
    * 货运单签收
    * @param reqVO
    */
   @Override
   @Transactional
   @LcnTransaction
   public void confirmVehicleDelivery(OrderVehicleDeliveryConfirmReqVO reqVO) {
      OrderVehicle orderVehicle = new OrderVehicle();
      orderVehicle.setOrderCarId(reqVO.getOrderCarId());
      orderVehicle.setCheckStatus(Constant.PMS_ORDER_VEHICLE_CHECK_STATUS_DEALER_CONFIRM);
      orderVehicleServiceImpl.updateById(orderVehicle);
   }



   @Override
   @Transactional
   @LcnTransaction
   public PageInfo listVehicleInStock(OrderVehicleInStockReqVO reqVO, Integer pageNum, Integer pageSize) {
      return orderVehicleServiceImpl.listVehicleInStock(reqVO,pageNum,pageSize);
   }

   /**
    * 整车入库
    * @param reqVO
    */
   @Override
   @Transactional
   @LcnTransaction
   public void updateVehicleInStockStatus(OrderVehicleInStockReqVO reqVO) {
      orderVehicleServiceImpl.updateVehicleInStockStatus(reqVO);
   }

   @Override
   public void band(String orderCustomerNo, String orderCarId) {
      //根据零售订单的渠道商code 获取地址
      OrderCustomerVO vo = orderCustomerService.getByOrderCustomerNo(orderCustomerNo);
      PartnerStorageVO ps = partnerStorageDubboServiceImpl.getByPartnerVO(vo.getPartnerCode());
      String addr = "";
      if(ps!=null){
         addr = ps.getAddr();
      }
      orderVehicleServiceImpl.band(orderCustomerNo,orderCarId,addr);
   }

   @Override
   public void unband(String orderCarId) {
      orderVehicleServiceImpl.unband(orderCarId);
   }

   @Override
   public void hireInit(String vin) {
      orderVehicleServiceImpl.hireInit(vin);
   }

   @Override
   public void hireCount(String vin) {
      orderVehicleServiceImpl.hireCount(vin);
   }

   @Override
   public void bandExp(String orderCustomerNo, String vin) {
      orderCustomerService.updateVinByOrderCutomerNo(orderCustomerNo,vin);
   }

   @Override
   @Transactional(rollbackFor = Exception.class)
   public ApiResult createDataForExpience(String vin) {
      return orderVehicleServiceImpl.createDataForExpience(vin);
   }
}
